<template>
  <div class="app-container">
    <div v-show="!detailInfo.visible">
      <div class="filter-wrapper">
        <NewPageFilter
          v-model:query="filterInfo.query.queryMap"
          :query-more-json="true"
          :filter-list="filterInfo.searchList"
          :filter-btn="filterInfo.searchBtn"
          :list-type-info="listTypeInfo"
          :clear-type="clearType"
        >
        </NewPageFilter>
      </div>
      <div class="filter-wrapper">
        <NewPageFilter :filter-btn="filterInfo.btnList"> </NewPageFilter>
      </div>
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query.queryMap"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-orderNoLike="scope">
          <span class="slip-edit" @click="showDetail(scope.row)">{{
            scope.row.orderNo || '-'
          }}</span>
        </template>
        <template v-slot:col-combinationNumLike="scope">
          <p>{{ scope.row.combinationNum || '-' }}</p>
        </template>
      </table-container>
    </div>

    <his-pre-export-order-detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :orderId="detailInfo.orderId"
      @returnBack="getList"
    />
  </div>
</template>

<script setup name="hisPreExportOrder">
import TableContainer from '@/components/TableContainer/index'
import { getPreExportOrderPage, postPreOrderThaw } from '@/api/his'
import { getDepotAll } from '@/api/core'
import HisPreExportOrderDetail from '@/views/project/sn/hisPreExportOrder/detail'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

const listTypeInfo = reactive({
  depotList: [],
  businessTypeList: [
    { key: '门诊', value: 'outpatient_service' },
    { key: '住院', value: 'in_hospital' },
  ],
  statusList: [],
})
const filterInfo = reactive({
  query: {
    sortString: '',
    customizeUserVO: {},
    queryMap: {},
  },
  searchList: [],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
  btnList: [],
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '解冻',
        event: thaw,
        show: true,
        ifRender: IfThaw,
      },
    ],
  },
})
//明细相关
const detailInfo = reactive({
  visible: false,
  orderId: '',
})
const clearType = ref(true)

let obj = {
  orderNo: {
    label: '预出单号',
    value: 'orderNoLike',
    asValue: 'orderNo',
    type: 'slot',
    componentType: 'input',
    event: search,
    show: true,
    hidden: false,
  },
  combinationNum: {
    label: '计费组合码',
    value: 'combinationNumLike',
    asValue: 'combinationNum',
    type: 'slot',
    componentType: 'input',
    event: search,
    show: true,
    hidden: false,
  },
  depotId: {
    label: '仓库',
    value: 'depotId',
    componentType: 'select',
    list: 'depotList',
    event: search,
    hidden: true,
    show: true,
  },
  depotCode: {
    label: '仓库编码',
    value: 'depotCode',
    hidden: false,
    show: false,
  },
  depotName: {
    label: '仓库名称',
    value: 'depotName',
    hidden: false,
    show: false,
  },
  businessType: {
    label: '业务类型',
    value: 'businessType',
    componentType: 'select',
    list: 'businessTypeList',
    event: search,
    hidden: true,
    show: true,
  },
  businessTypeValue: {
    label: '业务类型',
    value: 'businessTypeValue',
    hidden: false,
    show: false,
  },
  performedDeptCode: {
    label: '执行科室编码',
    value: 'performedDeptCode',
    show: false,
    hidden: true,
  },
  performedDeptName: {
    label: '执行科室名称',
    value: 'performedDeptName',
    show: false,
    hidden: true,
  },
  status: {
    label: '状态',
    value: 'status',
    componentType: 'select',
    list: 'statusList',
    event: search,
    hidden: true,
    show: true,
  },
  statusValue: {
    label: '状态',
    value: 'statusValue',
    hidden: false,
    show: false,
  },
  remark: {
    label: '备注',
    value: 'remark',
    hidden: false,
    show: true,
  },
}
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(obj),
})
filterInfo.searchList = Object.values({
  ...proxy.$fn.deepClone(obj),
})
getCustomize()

function thaw(row) {
  const ids = getIds(row)
  postPreOrderThaw(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function IfThaw(row) {
  return row.status !== 3
}
function getCustomize() {
  tableInfo.api = getPreExportOrderPage
  getDepotList()
  getList()
}
function getList() {
  tableInfo.refresh = Math.random()
}
function search() {
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
function clearSearch() {
  clearType.value = !clearType.value
  filterInfo.query.queryMap = proxy.$fn.deepClone(filterInfo.copyQueryMap)
  search()
}
function getDepotList(params = {}) {
  getDepotAll(params).then((resp) => {
    if (resp && resp.success) {
      const data = resp.data
      for (let item of data) {
        listTypeInfo.depotList.push({
          key: item.name,
          value: item.id,
        })
      }
    }
  })
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.selections = [...data]
      break
  }
}
function tableRowClassName({ row }) {
  return ''
}
//展示明细
function showDetail(row) {
  detailInfo.orderId = row.id
  detailInfo.visible = true
}
function getIds(row = undefined) {
  let ids = []
  if (row && row.id) {
    ids.push(row.id)
  } else {
    ids = tableInfo.selections.map((row) => row.id)
  }
  return ids
}
</script>

<style scoped lang="scss">
:deep(.el-table .nearly-row) {
  background: #fceacb;
}

:deep(.el-table .expire-row) {
  background: #f5e4e4;
}

// :deep(.el-table__body tr.hover-row > td) {
//   background-color: transparent !important;
// }
</style>
